home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 September / macformat-041.iso / mac / Shareware City / Graphics / MacSPD / Sources / tetra.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-30  |  4.7 KB  |  165 lines  |  [TEXT/MMCC]

  1. /*
  2.  * tetra.c - Create a tetrahedral pyramid.  This environment is based on the
  3.  *      scene used by Glassner ("Space Subdivision for Fast Ray Tracing," IEEE
  4.  *      CG&A, October 1984) and Kay & Kajiya ("Ray Tracing Complex Scenes,"
  5.  *      SIGGRAPH '86 Proceedings) for testing their ray tracers.
  6.  *      One light source.
  7.  *
  8.  * Author:  Eric Haines, 3D/Eye, Inc.
  9.  *
  10.  * Note:  the view and light positions are the same (after transformation to
  11.  *      a different set of world coordinates) as used by Kay & Kajiya,
  12.  *      courtesy of Tim Kay.  For some reason, the number of shadow rays
  13.  *      generated is different (Kay gets 34K, I get 46K).  One light source.
  14.  *
  15.  * size_factor determines the number of polygons output.
  16.  *      Total triangular polygons = 4**SF
  17.  *
  18.  *      size_factor     # triangles
  19.  *           1               4
  20.  *           2              16
  21.  *           3              64
  22.  *
  23.  *           6            4096
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <math.h>
  28. #include <stdlib.h>     /* atoi */
  29. #include "def.h"
  30. #include "drv.h"        /* display_close() */
  31. #include "lib.h"
  32.  
  33. static  int     size_factor = 6 ;
  34. static int raytracer_format = OUTPUT_RT_DEFAULT;
  35. static int output_format    = OUTPUT_CURVES;
  36.  
  37.  
  38. /* Create tetrahedrons recursively */
  39. static void
  40. create_tetra PARAMS(( int depth, COORD4 center ))
  41. {
  42.     int num_face, num_vert ;
  43.     int x_dir, y_dir, z_dir ;
  44.     int swap, vert_ord[3] ;
  45.     COORD3 face_pt[3], obj_pt[4] ;
  46.     COORD4 sub_center ;
  47.  
  48.     if ( depth <= 1 ) {
  49.     /* Output tetrahedron */
  50.  
  51.     PLATFORM_MULTITASK();
  52.  
  53.     /* find opposite corners of a cube which form a tetrahedron */
  54.     for ( num_vert = 0, x_dir = -1 ; x_dir <= 1 ; x_dir += 2 ) {
  55.         for ( y_dir = -1 ; y_dir <= 1 ; y_dir += 2 ) {
  56.         for ( z_dir = -1 ; z_dir <= 1 ; z_dir += 2 ) {
  57.             if ( x_dir*y_dir*z_dir == 1 ) {
  58.             obj_pt[num_vert][X] =
  59.                         center[X] + (double)x_dir * center[W] ;
  60.             obj_pt[num_vert][Y] =
  61.                         center[Y] + (double)y_dir * center[W] ;
  62.             obj_pt[num_vert][Z] =
  63.                         center[Z] + (double)z_dir * center[W] ;
  64.             ++num_vert ;
  65.             }
  66.         }
  67.         }
  68.     }
  69.  
  70.     /* find faces and output */
  71.     for ( num_face = 0 ; num_face < 4 ; ++num_face ) {
  72.         /* output order:
  73.          *   face 0:  points 0 1 2
  74.          *   face 1:  points 3 2 1
  75.          *   face 2:  points 2 3 0
  76.          *   face 3:  points 1 0 3
  77.          */
  78.         for ( num_vert = 0 ; num_vert < 3 ; ++num_vert ) {
  79.         vert_ord[num_vert] = (num_face + num_vert) % 4 ;
  80.         }
  81.         if ( num_face%2 == 1 ) {
  82.         swap = vert_ord[0] ;
  83.         vert_ord[0] = vert_ord[2] ;
  84.         vert_ord[2] = swap ;
  85.         }
  86.  
  87.         for ( num_vert = 0 ; num_vert < 3 ; ++num_vert ) {
  88.         COPY_COORD3( face_pt[num_vert], obj_pt[vert_ord[num_vert]] ) ;
  89.         }
  90.         lib_output_polygon( 3, face_pt ) ;
  91.     }
  92.  
  93.     }
  94.  
  95.     else {
  96.     /* Create sub-tetrahedra */
  97.  
  98.     /* find opposite corners of a cube to form sub-tetrahedra */
  99.     for ( x_dir = -1 ; x_dir <= 1 ; x_dir += 2 ) {
  100.         for ( y_dir = -1 ; y_dir <= 1 ; y_dir += 2 ) {
  101.         for ( z_dir = -1 ; z_dir <= 1 ; z_dir += 2 ) {
  102.             if ( x_dir*y_dir*z_dir == 1 ) {
  103.             sub_center[X] =
  104.                 center[X] + (double)x_dir * center[W] / 2.0 ;
  105.             sub_center[Y] =
  106.                 center[Y] + (double)y_dir * center[W] / 2.0 ;
  107.             sub_center[Z] =
  108.                 center[Z] + (double)z_dir * center[W] / 2.0 ;
  109.             sub_center[W] = center[W] / 2.0 ;
  110.  
  111.             create_tetra( depth-1, sub_center ) ;
  112.             }
  113.         }
  114.         }
  115.     }
  116.     }
  117. }
  118.  
  119. int
  120. main PARAMS(( int argc, char *argv[] ))
  121. {
  122.     COORD3  back_color, tetra_color, light ;
  123.     COORD3  from, at, up ;
  124.     COORD4  center_pt ;
  125.  
  126.     PLATFORM_INIT(SPD_TETRA);
  127.  
  128.     /* Start by defining which raytracer we will be using */
  129.     if ( lib_gen_get_opts( argc, argv,
  130.             &size_factor, &raytracer_format, &output_format ) ) {
  131.     return EXIT_FAIL;
  132.     }
  133.     if ( lib_open( raytracer_format, "Tetra.out" ) ) {
  134.     return EXIT_FAIL;
  135.     }
  136.  
  137.     /* output background color - UNC sky blue */
  138.     /* NOTE: Do this BEFORE lib_output_viewpoint(), for display_init() */
  139.     SET_COORD3( back_color, 0.078, 0.361, 0.753 ) ;
  140.     lib_output_background_color( back_color ) ;
  141.  
  142.     /* output viewpoint */
  143.     SET_COORD3( from, 1.022846, -3.177154, -2.174512 ) ;
  144.     SET_COORD3( at, -0.004103, -0.004103, 0.216539 ) ;
  145.     SET_COORD3( up, -0.816497, -0.816497, 0.816497 ) ;
  146.     lib_output_viewpoint( from, at, up, 45.0, 1.0, 1.0, 512, 512);
  147.  
  148.     /* output light source */
  149.     SET_COORD4( light, 1.876066, -18.123936, -5.000422, 1.0 ) ;
  150.     lib_output_light( light ) ;
  151.  
  152.     /* output tetrahedron color - red */
  153.     SET_COORD3( tetra_color, 1.0, 0.2, 0.2 ) ;
  154.     lib_output_color(NULL, tetra_color, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0);
  155.  
  156.     /* compute and output tetrahedral object */
  157.     SET_COORD4( center_pt, 0.0, 0.0, 0.0, 1.0 ) ;
  158.     create_tetra( size_factor, center_pt ) ;
  159.  
  160.     lib_close();
  161.  
  162.     PLATFORM_SHUTDOWN();
  163.     return EXIT_SUCCESS;
  164. }
  165.